# encoding: utf-8
class Advertise < ActiveRecord::Base 
  has_paper_trail :version => :paper_trail_version, :versions => :paper_trail_versions
  mount_uploader :cover, ImageUploader
  before_save :update_file_attributes
  belongs_to :advertise_group
  #validates :title, :cover, :presence => true

  def update_file_attributes
    if remove_cover == "1"
      self.content_type = nil
      self.file_size = nil
    else
      if cover.present? && cover_changed? 
        self.content_type = cover.file.content_type
        self.file_size = cover.file.size
      end
    end
  end

  def image
    host = Rails.application.config.default_url_options[:host]
    if position == 'landing'
      host + cover.url(:b)
    else
      host + cover.url(:m)
    end
  end

  def cover_dimensions
    "#{self.cover_image['height']} * #{self.cover_image['width']}" if cover_image
  end

  def cover_image
    @cover_image ||= MiniMagick::Image.open(cover.file.path) if cover.file
  end

end 
